<?php
/**
 * @package site
 */
require_once '_constants.php';

/**
 * Language manager class.
 *
 */
class SiteLang {
	private $lang;

	/**
	 * SiteLang constructor.
	 *
	 * @param string $ctxLangInfo - current language possibly extracted from the session or http headers.
	 */
	function __construct(string $ctxLangInfo) {
		$this->lang = is_string($ctxLangInfo) ? $ctxLangInfo : self::DEFAULT_LANG;
	}

	/**
	 * Mine language information from whatever indicates it.
	 *
	 * @return Mined lang info. - string
	 */
	static function mineLangInfo() {
		// if set in browser before
		$lang = @$_GET[Constants::REQ_LANG];
		// TODO get it from COOKIE / SESSION .. whatever
		if (empty($lang)) {
			$lang = self::examineHTTPLangArrayAndPickFeasibleOne(self::getHTTPLangHeader());
		}
		return empty($lang) ? self::DEFAULT_LANG : $lang;
	}
	
	/**
	 * Get Information about language from http headers.
	 * 
	 * @return Sorted array of prefered languages provided by client. - array
	 */
	static function getHTTPLangHeader() {
		$res = array();
		if (array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) {
			// Get comma separated language shortcuts.
			if (strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'], ',') !== false) {
				// More than 1 specified.
				$langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
			} else {
				// Only 1 specified.
				$langs = array($_SERVER['HTTP_ACCEPT_LANGUAGE']);
			}
			// For every specification evaluate its importance.
			foreach ($langs as $item) {
				if(strpos($item, ';') !== false) {
					// q variable is specified.
					$pos = explode(';', $item);
					$lang = trim($pos[0]);

					// Get q variable value.
					preg_match('/^q=([0-9.]+)$/', trim($pos[1]), $qval);
					$qpercent = intval(floatval($qval[1]) * 100);
				} else {
					// No q variable, importance is 1 ~ 100%.
					$qpercent = 100;
					$lang = trim($item);
				}
				// Set q value for every. 
				$res[$lang] = $qpercent;
			}
			// Sort the result.
			arsort($res);
		}
		return $res;
	}
	
	/**
	 * Available languages array.
	 * Semantics:
	 * 		"{language spec. - 2 chars} => array of language specs. which will stand for
	 *                                     language spec. specified in index.
	 */
	public static $LANG_AVAILABLE = array(
		"cs" => array("sk"), 
		"en" => array(), 
		"de" => array());
	const DEFAULT_LANG = "en";
	
	/**
	 * Examine Http Language converted array and pick feasible one (comparing
	 * to array self::$LANG_AVAILABLE.
	 *
	 * @param array $httpLangs - Sorted array of wanted languages. Specifications should be
	 * 		as indexes nor values.
	 * @return Value of one self::$LANG_AVAILABLE's index.
	 */
	static function examineHTTPLangArrayAndPickFeasibleOne(array $httpLangs) {
		foreach ($httpLangs as $httpLang => $tmp) {
			foreach (self::$LANG_AVAILABLE as $availLangKey => $availLangAdd) {
				if (strncmp($availLangKey, $httpLang, 2) === 0) {
					return $availLangKey;
				}
				foreach ($availLangAdd as $availLangAddVal) {
					if (strncmp($availLangAddVal, $httpLang, 2) === 0) {
						return $availLangKey;
					}
				}
			}
		}
		return self::DEFAULT_LANG;
	}

	/**
	 * Get current language.
	 *
	 * @return string
	 */
	function getLang() {
		return $this->lang;
	}
	
	/**
	 * if $lang is valid, use it.
	 *
	 * @see SiteLang::$LANG_AVAILABLE
	 * @param string $lang - language id to set
	 */
	function setLang($lang) {
		if (isset(self::$LANG_AVAILABLE[$lang]))
			$this->lang = $lang;
	}

}
?>